package com.summer.lock;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReadWriteLockTest {
	
	public static void main(String[] args) {
		Thread thread1 = new Thread(new Runnable() {
			@Override
			public void run() {
				for(int index =0;index<100000;index++){
					Cache.put(index+"", index);
				}
			}
		});
		Thread thread2 = new Thread(new Runnable() {
			@Override
			public void run() {
				for(int index =0;index<100000;index++){
					Cache.put(index+"", index+1);
				}
			}
		});
		Thread thread3 = new Thread(new Runnable() {
			@Override
			public void run() {
				for(int index =0;index<100000;index++){
					Cache.put(index+"", index+1);
				}
			}
		});
		Thread thread4 = new Thread(new Runnable() {
			@Override
			public void run() {
				for(int index =0;index<100000;index++){
					Cache.put(index+"", index+1);
				}
			}
		});
		Thread thread5 = new Thread(new Runnable() {
			@Override
			public void run() {
				for(int index =0;index<100000;index++){
					Cache.put(index+"", index);
				}
			}
		});
		Thread thread6 = new Thread(new Runnable() {
			@Override
			public void run() {
				for(int index =0;index<100000;index++){
					Cache.put(index+"", index+1);
				}
			}
		});
		Thread thread7 = new Thread(new Runnable() {
			@Override
			public void run() {
				for(int index =0;index<100000;index++){
					Cache.put(index+"", index+1);
				}
			}
		});
		Thread thread8 = new Thread(new Runnable() {
			@Override
			public void run() {
				for(int index =0;index<100000;index++){
					Cache.put(index+"", index+1);
				}
			}
		});
		Thread thread9 = new Thread(new Runnable() {
			@Override
			public void run() {
				for(int index =0;index<100000;index++){
					Cache.put(index+"", index);
				}
			}
		});
		Thread thread10 = new Thread(new Runnable() {
			@Override
			public void run() {
				for(int index =0;index<100000;index++){
					Cache.put(index+"", index+1);
				}
			}
		});
		Thread thread11 = new Thread(new Runnable() {
			@Override
			public void run() {
				for(int index =0;index<100000;index++){
					Cache.put(index+"", index+1);
				}
			}
		});
		Thread thread12 = new Thread(new Runnable() {
			@Override
			public void run() {
				for(int index =0;index<100000;index++){
					Cache.put(index+"", index+1);
				}
			}
		});
		Thread thread13 = new Thread(new Runnable() {
			@Override
			public void run() {
				for(int index =0;index<100000;index++){
					Cache.put(index+"", index);
				}
			}
		});
		Thread thread14 = new Thread(new Runnable() {
			@Override
			public void run() {
				for(int index =0;index<100000;index++){
					Cache.put(index+"", index+1);
				}
			}
		});
		Thread thread15 = new Thread(new Runnable() {
			@Override
			public void run() {
				for(int index =0;index<100000;index++){
					Cache.put(index+"", index+1);
				}
			}
		});
		Thread thread16 = new Thread(new Runnable() {
			@Override
			public void run() {
				for(int index =0;index<100000;index++){
					Cache.put(index+"", index+1);
				}
			}
		});
		
		
		Thread threadMian = new Thread(new Runnable() {
			@Override
			public void run() {
				long time = System.currentTimeMillis(); 
				for(int index =0;index<100000;index++){
					if(Cache.get(index+"")!=null)
					System.out.println(Cache.get(index+"").toString());
				}
				time = System.currentTimeMillis() - time;
			    System.out.println("oMethod: " + time + " ms"); 
			}
		});
		
		thread1.start();
		thread2.start();
		thread3.start();
		thread4.start();
		thread5.start();
		thread6.start();
		thread7.start();
		thread8.start();
		thread9.start();
		thread10.start();
		thread11.start();
		thread12.start();
		thread13.start();
		thread14.start();
		thread15.start();
		thread16.start();
		threadMian.start();
	}
	
	
	
	public static class Cache {
		static Map<String, Object> map = new HashMap<String, Object>();
		static ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
		static Lock r = rwl.readLock();
		static Lock w = rwl.writeLock();
		// 获取一个key对应的value
		public static final Object get(String key) {
			r.lock();
			try {
				return map.get(key);
			} finally {
				r.unlock();
			}
		}
		// 设置key对应的value，并返回旧有的value
		public static final Object put(String key, Object value) {
			w.lock();
			try {
				return map.put(key, value);
			} finally {
				w.unlock();
			}
		}
		// 清空所有的内容
		public static final void clear() {
			w.lock();
			try {
				map.clear();
			} finally {
				w.unlock();
			}
		}
	}
}
